#!/bin/bash

#
# refreshPgHba.sh
# created by youngeryoyo
# of qloudgen to refresh pg_bha on all nodes while adding or removing node
# 

if [ $# != 4 ];then
  echo "Usage: refreshPghba.sh configFile [add | remove] [ node | client ] ip"
  exit 1;
fi
configFile=$1
operaType=$2
nodeType=$3
ip=$4
access=md5

if [ "$operaType" != "add" ] && [ "$operaType" != "remove" ];then
  echo "the second parameter muster be add or remove only!"
  exit 2;
fi

if [ "$nodeType" != "node" ] && [ "$nodeType" != "client" ];then
  echo "the third parameter must be node or client only!"
  exit 3;
fi

if [ "$nodeType" = "node" ];then
  access=trust 
fi

# load configFile variables
if [ -f $configFile ]; then
  source $configFile
else
  echo "$configFile does not exist!"
  exit 3;
fi


checkIp () {
  regex="\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\b"
  check=`echo $1 | egrep $regex | wc -l`
  if [ $check -eq 0 ];then
    echo "The string $1 is not a correct ip!"
    exit 4;
  fi
}

# check ip
checkIp $ip

echo $access

# add new ip to pg_hba.conf of all nodes, and reload

for ((i=0; i<${#coordNames[@]}; i++));do
    
    if [ "${coordNames[$i]}" != "none" ] && [ "${coordNames[$i]}" != "N/A" ]; then
      echo Configuring ${coordMasterServers[$i]}:${coordMasterDirs[$i]}/pg_hba.conf

      if [ "$operaType" = "add" ];then
	ssh $pgxcUser@${coordMasterServers[$i]} "cat >> ${coordMasterDirs[$i]}/pg_hba.conf" <<EOF
host all $pgxcOwner $ip/32 $access 
host dilithium all $ip/32 md5
EOF
        if [ "$nodeType" = "node" ];then
	  if [ "$coordSlave" = "y" ] && [ "${coordSlaveServers[$i]}" != "none" ] && [ "${coordSlaveServers[$i]}" != "N/A" ]; then
	    ssh $pgxcUser@${coordMasterServers[$i]} "cat >> ${coordMasterDirs[$i]}/pg_hba.conf"  <<EOF
host replication $pgxcOwner $ip/32 trust
EOF
	  fi
        fi 
      else

        ssh $pgxcUser@${coordMasterServers[$i]} "sed -i \"/$ip/d\" ${coordMasterDirs[$i]}/pg_hba.conf"

      fi 
	
      # reload coordinator pg_hba.conf
      ssh $pgxcUser@${coordMasterServers[$i]} pg_ctl reload -Z coordinator -D ${coordMasterDirs[$i]}

    fi
	
done
	
for ((i=0; i<${#datanodeNames[@]}; i++));do
    if [ "${datanodeNames[$i]}" != "none" ] && [ "${datanodeNames[$i]}" != "N/A" ]; then
      echo Configuring ${datanodeMasterServers[$i]}:${datanodeMasterDirs[$i]}/pg_hba.conf
      if [ "$operaType" = "add" ];then
        if [ "$nodeType" = "node" ];then
          ssh $pgxcUser@${datanodeMasterServers[$i]} "cat >> ${datanodeMasterDirs[$i]}/pg_hba.conf" <<EOF
host all $pgxcOwner $ip/32 $access 
host dilithium all $ip/32 md5 
EOF
          if [ "$datanodeSlave" = "y" ] && [ "${datanodeSlaveServers[$i]}" != "none" ] && [ "${datanodeSlaveServers[$i]}" != "N/A" ]; then
	    ssh $pgxcUser@${datanodeMasterServers[$i]} "cat >> ${datanodeMasterDirs[$i]}/pg_hba.conf"  <<EOF
host replication $pgxcOwner $ip/32 trust
EOF
	  fi
        fi
      else

        ssh $pgxcUser@${datanodeMasterServers[$i]} "sed -i \"/$ip/d\" ${datanodeMasterDirs[$i]}/pg_hba.conf"

      fi

      # reload datanode pg_hba.conf
      ssh $pgxcUser@${datanodeMasterServers[$i]} pg_ctl reload -Z datanode -D ${datanodeMasterDirs[$i]}

   fi
     	
done

if [ "$operaType" = "add" ];then
#  pgHbaEntries+=($ip/32)
  coordPgHbaEntries+=($ip/32)
  datanodePgHbaEntries+=($ip/32)
else
#  pgHbaEntries=( ${pgHbaEntries[@]/$ip\/32/})
  coordPgHbaEntries=( ${coordPgHbaEntries[@]/$ip\/32/})
  datanodePgHbaEntries=( ${datanodePgHbaEntries[@]/$ip\/32/})
fi

cat >> $configFile <<EOF
# Update due to refresh pg_hba.conf
coordPgHbaEntries=( ${coordPgHbaEntries[@]} )
datanodePgHbaEntries=( ${datanodePgHbaEntries[@]} )
# End of update
EOF
